/**
* This file is part of OntoApplications (project linked with OntoCompoFramework)
*
* Copyright (C) 2011 -  Christian Brel
*
* @author   Main    Christian Brel
**/
package applications.businessdirectory.ui.lib;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;

import ontocompo.lib.PartialBorder;
import ontocompo.lib.SpringUtilities;
import ontocompo.lib.PartialBorder.Side;

import org.objectweb.fractal.fraclet.annotations.Component;
import org.objectweb.fractal.fraclet.annotations.Interface;
import org.objectweb.fractal.fraclet.annotations.Requires;

import applications.businessdirectory.fc.api.BusinessCard;
import applications.businessdirectory.fc.api.Post;
import applications.insurancecard.fc.api.InsuranceInformation;

@Component(provides = {@Interface(name = "jPanelItf", signature = org.objectweb.fractal.swing.JPanelItf.class), @Interface(name = "jPanelAttributes", signature = org.objectweb.fractal.swing.JPanelAttributes.class), @Interface(name = "uicontainerservice", signature = ontocompo.api.UIContainerService.class), @Interface(name = "observer", signature = java.util.Observer.class), @Interface(name = "ontocompointrospection", signature = ontocompo.api.OntoCompoIntrospection.class)})
public class BusinessDirectoryVisitCardPanel extends JPanel implements /*ContainerService,*/ org.objectweb.fractal.swing.JPanelItf, org.objectweb.fractal.swing.JPanelAttributes, ontocompo.api.UIContainerService, java.util.Observer, ontocompo.api.OntoCompoIntrospection {

	private String uid;
	private org.objectweb.fractal.api.Component c;
	
	@Requires(name = "cardinfonamevalue")
	private ontocompo.api.UIContainerService cardinfonamevalue;
	
	@Requires(name = "cardinfoemaillabel")
	private ontocompo.api.UIContainerService cardinfoemaillabel;
	
	@Requires(name = "cardinfoemailvalue")
	private ontocompo.api.UIContainerService cardinfoemailvalue;
	
	@Requires(name = "cardinfocardlinkvalue")
	private ontocompo.api.UIContainerService cardinfocardlinkvalue;
	
	
	
	@Requires(name = "businessdirectoryservice")
	private applications.businessdirectory.fc.api.BusinessDirectoryService businessdirectoryservice;
	
	@Requires(name = "businessdirectoryobservable")
	private ontocompo.api.Observable businessdirectoryobservable;
	
	public BusinessDirectoryVisitCardPanel() {
		super();
		this.uid = "applications.businessdirectory.ui.lib.BusinessDirectoryVisitCardPanel";
		System.out.println(this.toString() + " created");
		//this.init();
	}
	
	public void init() {
		this.businessdirectoryobservable.addObserver(this);
		
		this.cardinfonamevalue.init();
		this.cardinfoemaillabel.init();
		this.cardinfoemailvalue.init();
		this.cardinfocardlinkvalue.init();
		
		this.setBackground(Color.white);
		
		PartialBorder pb = new PartialBorder();
		pb.setPadding(10);
		pb.setSide(Side.BOTTOM, true, 1);
		this.setBorder(pb);
		
//		JLabel vcLink = generateLink(" ### ");
		
		JPanel lastline = createBox(0);
		lastline.setLayout(new BoxLayout(lastline, BoxLayout.X_AXIS));		
//		JLabel em = createTd("E-mail : ");
//		JLabel mailto = generateLink(" ### ");
		
		lastline.add(Box.createHorizontalGlue());
		
		lastline.add(this.cardinfoemaillabel.getComponentItSelf());
		lastline.add(this.cardinfoemailvalue.getComponentItSelf());
		
		JComponent[] lines = { this.cardinfonamevalue.getComponentItSelf(),
							this.cardinfocardlinkvalue.getComponentItSelf(),
						      lastline
						    };
		
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		SequentialGroup sg = layout.createSequentialGroup();
		
		for(JComponent jc : lines)
		{
			sg.addComponent(jc);
		}
		layout.setVerticalGroup(sg);
		
		sg = layout.createSequentialGroup();
		ParallelGroup pg = layout.createParallelGroup(GroupLayout.Alignment.LEADING);		
		for(JComponent jc : lines)
		{
				pg.addComponent(jc);
		}
		sg.addGroup(pg);
		layout.setHorizontalGroup(sg);
	}
	
	private JComponent generateAddress(String street, String town, String country)
	{
		JPanel detail = createBox(0);
		GridBagLayout dbl = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		detail.setLayout(dbl);
		c.gridx = 0;
		c.gridy = 0;
		c.anchor = GridBagConstraints.CENTER;
		detail.add(createTh("Street"), c);
		c.gridx = 1;
		c.anchor = GridBagConstraints.WEST;
		detail.add(createTd(street), c);
		
		c.gridx = 0;
		c.gridy = 1;
		c.anchor = GridBagConstraints.CENTER;

		detail.add(createTh("Zip Code"), c);
		
		c.gridx = 1;
		c.anchor = GridBagConstraints.WEST;

		detail.add(createTd(town), c);
		
		c.gridx = 0;
		c.gridy = 2;
		c.anchor = GridBagConstraints.CENTER;

		detail.add(createTh("Country"), c);
		c.gridx = 1;	
		c.weightx = 1;
		c.anchor = GridBagConstraints.WEST;
		detail.add(createTd(country), c);
		
		return detail;
	}
	
	private JLabel generateLink(String txt)
	{
		JLabel link = createTd("<html><u>"+txt+"</u></html>");
		link.setForeground(Color.blue);
		link.setCursor(new Cursor(Cursor.HAND_CURSOR));
		
		return link;
	}
	
	private JPanel createUnderlinedBox(float width)
	{
		JPanel result = createBox(0);
		PartialBorder pb = new PartialBorder();
		pb.setPadding(10);
		pb.setSide(Side.BOTTOM, true, width);
		result.setBorder(pb);
		
		return result;
	}
	
	protected JPanel createBox(int thickness)
	{
		JPanel result = new JPanel();
		if (thickness > 0) result.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
		result.setBackground(Color.white);
		return result;
	}
	
	protected JLabel createTd(String txt)
	{
		JLabel result = new JLabel(txt);
		result.setFont(result.getFont().deriveFont(Font.PLAIN));
		result.setHorizontalAlignment(SwingConstants.LEFT);

		return result;
	}
	
	protected JLabel createTd(String txt, int thickness)
	{
		JLabel result = createTd(txt);
		result.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
		return result;
	}
	
	protected JLabel createTh(String txt)
	{
		JLabel result = new JLabel(txt);
		// result.setBorder(BorderFactory.createLineBorder(Color.blue, 1));

		result.setHorizontalAlignment(SwingConstants.CENTER);
		return result;
	}
	
	protected JLabel createTh(String txt, int thickness)
	{
		JLabel result = createTh(txt);
		result.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
		return result;
	}
	
	protected JLabel createTitle(String txt)
	{
		JLabel result = new JLabel(txt);
		result.setHorizontalAlignment(SwingConstants.LEFT);
		result.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		return result;
	}
	
	protected JPanel createLine(int axis, int thickness, boolean title, String... msgs)
	{
		JPanel result = createBox(thickness);
		result.setLayout(new BoxLayout(result, axis));
		for(String s : msgs)
		{
			String msg = s;
			if (s == "") msg = " ";
			
			JLabel lbl ;
			if (title) lbl = createTh(msg);
			else lbl = createTd(msg);
			
			lbl.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
			lbl.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
			
			result.add(lbl);
		}
		
		return result;
	}
	
	protected JPanel createLineLeftAlign(int axis, int thickness, boolean title, String... msgs)
	{
		JPanel result = createBox(thickness);
		result.setLayout(new BoxLayout(result, axis));
		for(String s : msgs)
		{
			String msg = s;
			if (s == "") msg = " ";
			
			JLabel lbl ;
			if (title) lbl = createTh(msg);
			else lbl = createTd(msg);
			
			lbl.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
			lbl.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
			
			result.add(lbl);
		}
		
		return result;
	}
	
//	@Override
	public JComponent getComponentItSelf() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public String getUid() {
		// TODO Auto-generated method stub
		return this.uid;
	}

	@Override
	public void setUid(String uid) {
		// TODO Auto-generated method stub
		this.uid = uid;
	}
	
	public String toString() {
		return "BusinessDirectoryVisitCardPanel";
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		BusinessCard bc = this.businessdirectoryservice.getBusinessCard();
		if(bc == null) {
			return ;
		} else {
			this.update(bc);
		}
	}

	@Override
	public void update(Object o) {
		// TODO Auto-generated method stub
		BusinessCard bc = (BusinessCard) o;
		
		this.cardinfonamevalue.update("<html>" + bc.getStatus() + "<strong>" + bc.getName() + "</strong></html>");
		this.cardinfoemailvalue.update(bc.getEmail());
		this.cardinfocardlinkvalue.update(bc.getCardLink());
		
		this.removeAll();
		
		this.setBackground(Color.white);
		
		PartialBorder pb = new PartialBorder();
		pb.setPadding(10);
		pb.setSide(Side.BOTTOM, true, 1);
		this.setBorder(pb);
		
//		JLabel vcLink = generateLink(" ### ");
		
		JPanel lastline = createBox(0);
		lastline.setLayout(new BoxLayout(lastline, BoxLayout.X_AXIS));		
//		JLabel em = createTd("E-mail : ");
//		JLabel mailto = generateLink(" ### ");
		
		lastline.add(Box.createHorizontalGlue());
		
		lastline.add(this.cardinfoemaillabel.getComponentItSelf());
		lastline.add(this.cardinfoemailvalue.getComponentItSelf());
		
		JComponent[] lines = { this.cardinfonamevalue.getComponentItSelf(),
							this.cardinfocardlinkvalue.getComponentItSelf(),
						      lastline
						    };
		
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		SequentialGroup sg = layout.createSequentialGroup();
		
		for(JComponent jc : lines)
		{
			sg.addComponent(jc);
		}
		layout.setVerticalGroup(sg);
		
		sg = layout.createSequentialGroup();
		ParallelGroup pg = layout.createParallelGroup(GroupLayout.Alignment.LEADING);		
		for(JComponent jc : lines)
		{
				pg.addComponent(jc);
		}
		sg.addGroup(pg);
		layout.setHorizontalGroup(sg);
		
		this.getParent().validate();
	}
	
	@Override
	public org.objectweb.fractal.api.Component getFractalComponent() {
		// TODO Auto-generated method stub
		return this.c;
	}

	@Override
	public void setFractalComponent(org.objectweb.fractal.api.Component c) {
		// TODO Auto-generated method stub
		this.c = c;
	}

}
